package com.itany.corejava.day15;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class LanguageUtil {

    private static Map<String,String> mapper=new HashMap<>();

    static{
        try(BufferedReader br=new BufferedReader(
               new InputStreamReader(
                       LanguageUtil.class
                                   .getClassLoader()
                                   .getResourceAsStream("com/itany/corejava/day15/config.txt")
               )
        )){

            String s;

            while((s=br.readLine())!=null){
                String[] entry=s.split(":");
//                System.out.println(Arrays.toString(entry));
                mapper.put(entry[0],entry[1]);
            }



        }catch (Exception e){
            e.printStackTrace();
            throw new ExceptionInInitializerError("LanguageUtil初始化错误");
        }
    }


    public static void main(String[] args) {

        parse("C:\\Users\\Administrator\\Desktop\\新建文件夹\\aa.txt",
                "C:\\Users\\Administrator\\Desktop\\新建文件夹\\Person.java");


    }

    public static void parse(String oldFile,String newFile){

        try(BufferedReader br=new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(oldFile),"utf-8")
            );
            PrintWriter pw=new PrintWriter(newFile,"utf-8");
        ){


            String s;

            StringBuilder builder=new StringBuilder();
            while((s=br.readLine())!=null){
                builder.append(s).append("\n");
            }

            String code=builder.toString();


//            System.out.println(code.toString());


            Set<Map.Entry<String,String>> entrySet=mapper.entrySet();

            for (Map.Entry<String, String> entry : entrySet) {
                code=code.replace(entry.getKey(),entry.getValue());
            }


//            mapper.forEach((k,v)->{
//                code.replace(k,v);
//            });

            System.out.println(code);

            pw.print(code);

        }catch (Exception e){
            e.printStackTrace();
        }


    }

}
